<?php

/**
 * 代理等级关系管理
 *
 * @author Alen
 * @since 1.0.0 2013-12-17 
 */
class AC_Relation_ManageAction extends PyramidACAction {

    //put your code here
    public function index() {
        if(!$this->USER['TradePassword']) redirect('未设置交易密码','?m=UC_TradePassword_Edit');
        
        $tip = '您有 <strong style="color:#F60;">' . $this->USER['QuotaNumber'] . '</strong> 个预购席位。';
        $tip.=($this->USER['QuotaNumber'] ? '<a href="###" id="lnkAddSeat">[开通席位]</a> <a href="?m=AC_Promotor">[邀请席位]</a>' : '预购席位不足，您将无法激活的会员! <a href="?m=AC_Preorderseat">[预购席位]</a>');
        $this->assign('tip', $tip);
        if(isset($_SESSION['tradepwd'])){
            $this->assign('table', $this->get_table());   
        }
        $this->assign('check_pwd', isset($_SESSION['tradepwd']));
        $this->display();
    }
    
    public function checkpwd(){   
        $this->check_tradepassword();
    }

    private function get_table() {
        $str = '<table cellpadding="0" cellspacing="0" class="list_table m_10" width="100%">';
        $str.='<thead><tr><th>用户名</th><th>状态</th><th>开通时间</th><th>过期时间</th><th>操作</th></tr></thead><tbody>';
        $bizuser = new SoBizUser();
        $rs = $bizuser->select_arrayset("ReferrerName='$this->USER_NAME'");
        if ($rs) {
            $i1=0; $i2=0; $i3=0; $i4=0; $i5=0;
            if (!$this->biz)
                $this->biz = new SoBizCommon ();
            foreach ($rs as $row) {
                $i1++;
                $str.= '<tr class="lvl1"><td class="name em">' . $row['UserName'] . '</td><td>' . $this->biz->get_state_name($row['State']) . '</td><td>' . $row['CreatedTime'] . '</td><td>' . $row['RenewalExpiresTime'] . '</td><td class="optCell">' . ($row['IsAgent'] ? '已激活' : '<a href="?m=AC_Relation_Manage&action=active&u=' . $row['UserName'] . '" onclick="return confirm(\'请确认已经收到下级的汇款后激活，您确定要激活吗？\');">[激活]</a>') . '&nbsp;</td></tr>';
                $rs2 = $bizuser->select_arrayset('ReferrerName=\'' . $row['UserName'] . '\'');
                if ($rs2) {
                    foreach ($rs2 as $row2) {
                    $i2++;
                        $str.= '<tr class="lvl2"><td class="name em">' . $row2['UserName'] . '</td><td>' . $this->biz->get_state_name($row2['State']) . '</td><td>' . $row2['CreatedTime'] . '</td><td>' . $row2['RenewalExpiresTime'] . '</td><td class="optCell">' . ($row2['IsAgent'] ? '已激活' : '<a href="?m=AC_Relation_Manage&action=active&u=' . $row2['UserName'] . '" onclick="return confirm(\'请确认已经收到下级的汇款后激活，您确定要激活吗？\');">[激活]</a>') . '&nbsp;</td></tr>';
                        $rs3 = $bizuser->select_arrayset('ReferrerName=\'' . $row2['UserName'] . '\'');
                        if ($rs3) {
                            foreach ($rs3 as $row3) {
                            $i3++;
                                $str.= '<tr class="lvl3"><td class="name em">' . $row3['UserName'] . '</td><td>' . $this->biz->get_state_name($row3['State']) . '</td><td>' . $row['CreatedTime'] . '</td><td>' . $row3['RenewalExpiresTime'] . '</td><td class="optCell">' . ($row3['IsAgent'] ? '已激活' : '<a href="?m=AC_Relation_Manage&action=active&u=' . $row3['UserName'] . '" onclick="return confirm(\'请确认已经收到下级的汇款后激活，您确定要激活吗？\');">[激活]</a>') . '&nbsp;</td></tr>';
                                $rs4 = $bizuser->select_arrayset('ReferrerName=\'' . $row3['UserName'] . '\'');
                                if ($rs4) {
                                    $i4++;
                                    foreach ($rs4 as $row4) {
                                        $str.= '<tr class="lvl4"><td class="name em">' . $row4['UserName'] . '</td><td>' . $this->biz->get_state_name($row4['State']) . '</td><td>' . $row4['CreatedTime'] . '</td><td>' . $row4['RenewalExpiresTime'] . '</td><td class="optCell">' . ($row4['IsAgent'] ? '已激活' : '<a href="?m=AC_Relation_Manage&action=active&u=' . $row4['UserName'] . '" onclick="return confirm(\'请确认已经收到下级的汇款后激活，您确定要激活吗？\');">[激活]</a>') . '&nbsp;</td></tr>';
                                        $rs5 = $bizuser->select_arrayset('ReferrerName=\'' . $row4['UserName'] . '\'');
                                        if ($rs5) {
                                            $i5++;
                                            foreach ($rs5 as $row5) {
                                                $str.= '<tr class="lvl5"><td class="name em">' . $row5['UserName'] . '</td><td>' . $this->biz->get_state_name($row5['State']) . '</td><td>' . $row5['CreatedTime'] . '</td><td>' . $row5['RenewalExpiresTime'] . '</td><td class="optCell">' . ($row5['IsAgent'] ? '已激活' : '<a href="?m=AC_Relation_Manage&action=active&u=' . $row5['UserName'] . '" onclick="return confirm(\'请确认已经收到下级的汇款后激活，您确定要激活吗？\');">[激活]</a>') . '&nbsp;</td></tr>';
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $str.='</tbody></table>';
        // 100, 50, 30, 20, 10
        $summary='<div style="line-height:24px; font-size:13px; color:#F60; padding:10px;">一级代理 <strong>'.$i1.'</strong> 人， 获利 <strong>100 * '.$i1.'</strong>=<strong>'.($i1 * 100).'</strong> 元<br />';
        $summary.='二级代理 <strong>'.$i2.'</strong> 人， 获利 <strong>50 * '.$i2.'</strong>=<strong>'.($i2 * 50).'</strong> 元<br />';
        $summary.='三级代理 <strong>'.$i3.'</strong> 人， 获利 <strong>30 * '.$i3.'</strong>=<strong>'.($i3 * 30).'</strong> 元<br />';
        $summary.='四级代理 <strong>'.$i4.'</strong> 人， 获利 <strong>20 * '.$i4.'</strong>=<strong>'.($i4 * 20).'</strong> 元<br />';
        $summary.='五级代理 <strong>'.$i5.'</strong> 人， 获利 <strong>10 * '.$i5.'</strong>=<strong>'.($i5 * 10).'</strong> 元<br />';
        $summary.='合计：<strong>'.($i1*100+$i2*50+$i3*30+$i4+20+$i5*10).'</strong> 元</div>';
        return $summary.$str;
    }

    /**
     * 开通会员
     */
    public function save() {
        $bizuser = new SoBizUser();
        if (!$bizuser->select_value("UserName='$this->USER_NAME'", 'QuotaNumber'))
            redirect('您的预购席位不足，请先预购席位', '?m=AC_Relation_Manage&r=fail');
        $msg = $bizuser->add_agent(request_string('username'), request_string('password'), request_string('repassword'), $this->USER_NAME);
        redirect($msg == 'SUCCESS' ? '开通成功' : $msg);
    }
    
    /**
     * 激活会员
     */
    public function active(){
        $bizuser=new SoBizUser();
        $msg=$bizuser->active_agent(request_string('u'),$this->USER_NAME);
        redirect($msg=='SUCCESS'?'用户['.  request_string('u').']激活成功':$msg,'?m=AC_Relation_Manage');
    }

}
